package com.example.jjc.rxjava_retrofit2_demo.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.jjc.rxjava_retrofit2_demo.model.Item;

import java.util.List;

/**
 * 图片viewPager适配器
 * Created by Administrator on 2017/3/21.
 */

public class ImagePageAdapter extends PagerAdapter {
    List<Item> list;
    Context context;
    public ImagePageAdapter(Context context,List<Item> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        Glide.with(context)
                .load(list.get(position).image_url)
                .into(imageView);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
